#!/bin/sh
# LambdaNative - a cross-platform Scheme framework
# Copyright (c) 2009-2023, University of British Columbia
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the
# following conditions are met:
#
# * Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# * Neither the name of the University of British Columbia nor
# the names of its contributors may be used to endorse or
# promote products derived from this software without specific
# prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

SYS_PLATFORM_VARIANT="-api${SYS_ANDROIDAPI}"
if [ "X$ANDROIDARCH" = "Xarm64" ]; then
  android_cross=`ls -1d $ANDROIDNDK/toolchains/llvm/prebuilt/*/bin/aarch64*$SYS_ANDROIDAPI-clang | head -n 1`
else
  android_cross=`ls -1d $ANDROIDNDK/toolchains/llvm/prebuilt/*/bin/${ANDROIDARCH}*$SYS_ANDROIDAPI-clang | head -n 1`
fi
android_chainpath=`dirname "$android_cross"`/
SYS_CC=$android_cross" $SYS_DEBUGFLAG -fPIC -DANDROID $cflag_additions"
SYS_CXX=$android_cross"++ $SYS_DEBUGFLAG -fPIC -DANDROID $cflag_additions"
SYS_CPP=$android_cross" -E"
SYS_AR=$android_chainpath"llvm-ar"
SYS_AS=$android_chainpath"llvm-as"
SYS_NM=$android_chainpath"llvm-nm"
SYS_LD=$android_chainpath"lld"
SYS_RANLIB=$android_chainpath"llvm-ranlib"
SYS_STRIP=$android_chainpath"llvm-strip"
SYS_OBJCOPY=$android_chainpath"llvm-objcopy"
SYS_OBJDUMP=$android_chainpath"llvm-objdump"
SYS_READELF=$android_chainpath"llvm-readelf"
SYS_GPROF=
SYS_WINDRES=
SYS_EXEFIX=
SYS_APPFIX=

if [ -f "$android_cross" ]; then
  vecho "Using pre-built Android toolchain"
  return;
fi

# search for a working API using the legacy approach
android_apichanged=
while [ ${SYS_ANDROIDAPI} -ge 3 ]; do
  android_APIdir=`echo $ANDROIDNDK/platforms/android-${SYS_ANDROIDAPI}/arch-${ANDROIDARCH}`
  if [ -d "$android_APIdir" ] ; then
    break
  fi
  SYS_ANDROIDAPI=`expr ${SYS_ANDROIDAPI} - 1`
  android_apichanged="** WARNING Android API changed to $SYS_ANDROIDAPI"
done
assertfile "$android_APIdir"
if [ ! "X$android_apichanged" = "X" ]; then
  echo "$android_apichanged"
fi

if [ "X$ANDROIDARCH" = "Xarm64" ]; then
  android_chainpath=`ls -1d $ANDROIDNDK/toolchains/aarch64*-4.* | head -n 1`
else
  android_chainpath=`ls -1d $ANDROIDNDK/toolchains/${ANDROIDARCH}*-4.* | head -n 1`
fi
android_chain=`basename "$android_chainpath"`
android_ndkver=`basename "$ANDROIDNDK"`
android_customtoolchain=${SYS_PREFIX}/${android_ndkver}-${ANDROIDARCH}-${SYS_ANDROIDAPI}-toolchain
if [ ! -d $android_customtoolchain ]; then
  $ANDROIDNDK/build/tools/make-standalone-toolchain.sh \
     --toolchain=${android_chain} \
     --arch=$ANDROIDARCH \
     --platform=android-${SYS_ANDROIDAPI} \
     --install-dir=$android_customtoolchain
fi
assertfile $android_customtoolchain
android_sysroot="$android_customtoolchain/sysroot"
android_cross=`ls -1 $android_customtoolchain/bin/*-gcc 2> /dev/null | sed 's/gcc$//'`
LN_C_compiler=clang
SYS_CC=$android_cross"$LN_C_compiler $SYS_DEBUGFLAG -fPIC -DANDROID -D__ANDROID_API__=${SYS_ANDROIDAPI} -isysroot $android_sysroot $cflag_additions"
SYS_CXX=$android_cross"${LN_C_compiler}++ $SYS_DEBUGFLAG -fPIC -DANDROID -D__ANDROID_API__=${SYS_ANDROIDAPI} -isysroot $android_sysroot $cflag_additions"
SYS_CPP=$android_cross"$LN_C_compiler -E"
SYS_AR=$android_cross"ar"
SYS_AS=$android_cross"as"
SYS_NM=$android_cross"nm"
SYS_LD=$android_cross"ld"
SYS_RANLIB=$android_cross"ranlib"
SYS_STRIP=$android_cross"strip"
SYS_OBJCOPY=$android_cross"objcopy"
SYS_OBJDUMP=$android_cross"objdump"
SYS_READELF=$android_cross"readelf"
SYS_GPROF=$android_cross"gprof"
SYS_WINDRES=
SYS_EXEFIX=
SYS_APPFIX=

#eof
